import ipaddress

from . import db


class User(db.Model):
    __tablename__ = "sh_users"

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, default="", doc="用户名")
    phone = db.Column(db.String(256), unique=True, default="", doc="手机号")
    password = db.Column(db.String(256), unique=True, default="", doc="密码")
    email = db.Column(db.String(256), unique=True, default="", doc="邮箱")
    address = db.relationship(
        "ShopAddress", backref=db.backref("user", lazy="joined"), lazy="dynamic"
    )


class ShopAddress(db.Model):
    __tablename__ = "sh_shop_address"
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("sh_users.id"))

    number = db.Column(db.Integer, unique=True, default=0, doc="地区编号")
    province = db.Column(db.String(64), default="", doc="省")
    city = db.Column(db.String(64), default="", doc="市")
    area = db.Column(db.String(64), default="", doc="区")
    details = db.Column(db.String(256), default="", doc="详细地址")
    default = db.Column(db.Boolean, default=False, doc="默认")
    deleted = db.Column(db.Boolean, default=False, doc="删除")


class Address(db.Model):
    __tablename__ = "sh_address"
    id = db.Column(db.Integer, primary_key=True)
    parent = db.Column(db.Integer, unique=True, doc="父级编码")
    number = db.Column(db.Integer, unique=True, default=0, doc="区域编码")
    address = db.Column(db.String(64), doc="地址")
